package Day2025_03_14;

public class LeetCode978 {
    public int maxTurbulenceSize(int[] arr) {
        int n = arr.length;
        if (n == 1) return 1;
        int[] dp = new int[n];
        dp[0] = 1;
        if (arr[0] != arr[1]) dp[1] = 2;
        else dp[1] = 1;
        for (int i = 2; i < n; i++) {
            int x = arr[i - 2], y = arr[i - 1], z = arr[i];
            if ((x < y && y > z) || (x > y && y < z)) dp[i] = dp[i - 1] + 1;
            else if (y == z) dp[i] = 1;
            else dp[i] = 2;
        }
        int ret = 0;
        for (int x : dp) ret = Math.max(ret, x);
        return ret;
    }

    public static void main(String[] args) {
        LeetCode978 leetCode978 = new LeetCode978();
        System.out.println(leetCode978.maxTurbulenceSize(new int[]{9, 4, 2, 10, 7, 8, 8, 1, 9}));
    }
}
